<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.config;

import ch.qos.logback.classic.LoggerContext;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.jhipster.config.JHipsterProperties;
import org.slf4j.LoggerFactory;
<%_ if (serviceDiscoveryType === 'consul' || (serviceDiscoveryType && ['microservice', 'gateway', 'uaa'].includes(applicationType))) { _%>
import org.springframework.beans.factory.ObjectProvider;
<%_ } _%>
import org.springframework.beans.factory.annotation.Value;
<%_ if (serviceDiscoveryType && ['microservice', 'gateway', 'uaa'].includes(applicationType)) { _%>
import org.springframework.boot.info.BuildProperties;
<%_ } _%>
<%_ if (serviceDiscoveryType === "consul") { _%>
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
<%_ } _%>
<%_ if (["eureka", "consul"].includes(serviceDiscoveryType)) { _%>
import org.springframework.cloud.context.config.annotation.RefreshScope;
<%_ } _%>
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static io.github.jhipster.config.logging.LoggingUtils.*;

/*
 * Configures the console and Logstash log appenders from the app properties
 */
@Configuration
<%_ if (["eureka", "consul"].includes(serviceDiscoveryType)) { _%>
@RefreshScope
<%_ } _%>
public class LoggingConfiguration {

    public LoggingConfiguration(@Value("${spring.application.name}") String appName,
                                @Value("${server.port}") String serverPort,
                                JHipsterProperties jHipsterProperties,
                                <%_ if (serviceDiscoveryType === "consul") { _%>
                                ObjectProvider<ConsulRegistration> consulRegistration,
                                <%_ } _%>
                                <%_ if (serviceDiscoveryType && ['microservice', 'gateway', 'uaa'].includes(applicationType)) { _%>
                                ObjectProvider<BuildProperties> buildProperties,
                                <%_ } _%>
                                ObjectMapper mapper) throws JsonProcessingException {

        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

        Map<String, String> map = new HashMap<>();
        map.put("app_name", appName);
        map.put("app_port", serverPort);
        <%_ if (serviceDiscoveryType && ['microservice', 'gateway', 'uaa'].includes(applicationType)) { _%>
        buildProperties.ifAvailable(it -> map.put("version", it.getVersion()));
        <%_ } _%>
        <%_ if (serviceDiscoveryType === 'consul') { _%>
        consulRegistration.ifAvailable(it -> map.put("instance_id", it.getInstanceId()));
        <%_ } _%>
        String customFields = mapper.writeValueAsString(map);

        JHipsterProperties.Logging loggingProperties = jHipsterProperties.getLogging();
        JHipsterProperties.Logging.Logstash logstashProperties = loggingProperties.getLogstash();

        if (loggingProperties.isUseJsonFormat()) {
            addJsonConsoleAppender(context, customFields);
        }
        if (logstashProperties.isEnabled()) {
            addLogstashTcpSocketAppender(context, customFields, logstashProperties);
        }
        if (loggingProperties.isUseJsonFormat() || logstashProperties.isEnabled()) {
            addContextListener(context, customFields, loggingProperties);
        }
        if (jHipsterProperties.getMetrics().getLogs().isEnabled()) {
            setMetricsMarkerLogbackFilter(context, loggingProperties.isUseJsonFormat());
        }
    }
}
